//
//  HQTableViewController.m
//  OASystem
//
//  Created by zhoupengfei on 15/11/2.
//  weakright © 2015年 zpf. All rights reserved.
//

#import "HQTableViewController.h"
#import "MBProgressHUD.h"
#import "HttpTools.h"
#import "ProcessModel.h"
#import "GDataDefines.h"
#import "OAAttachmentsModel.h"
#import "Toast+UIView.h"
#import "UtilTool.h"
#import "UIAlertView+Blocks.h"
#import "OASelectDepartmentController.h"
#import "OADepartmentModel.h"
#import <QuickLook/QuickLook.h>
@interface HQTableViewController ()<QLPreviewControllerDataSource,QLPreviewControllerDelegate>
@property(nonatomic,strong)  ProcessModel * detailModel ;
@property(nonatomic,strong)NSMutableArray * attachmentArray; //附件
@property(nonatomic,strong)NSMutableArray * mainattachmentArray; //正文附件
@property(nonatomic,copy)NSString * textString;


@property(nonatomic,weak) IBOutlet UILabel  * titleLabel; //主题
@property(nonatomic,weak) IBOutlet UILabel  * numberLabel; //编号
@property(nonatomic,weak) IBOutlet UILabel  * senderDepartmentLabel; //发出部门
@property(nonatomic,weak) IBOutlet UILabel  * recerDepartmentLabel;//发往部门
@property(nonatomic,weak) IBOutlet UILabel  * processTypeLabel; //合同类型 /支付类型
@property (weak, nonatomic) IBOutlet UILabel *ContractNameLabel;

@property(nonatomic,weak) IBOutlet UILabel  * emergencyLevelLabel; //紧急程度
@property(nonatomic,weak) IBOutlet UILabel  * contentsLabel; //内容
@property(nonatomic,weak)IBOutlet  UILabel  * tipsLabel;//备注
@property(nonatomic,weak)IBOutlet  UILabel  * finalStaffLabel; //预决算人员
@property(nonatomic,weak)IBOutlet  UILabel  * handleDepartmentLabel; //经办部门负责人
@property(nonatomic,weak)IBOutlet  UILabel  * officeDepartmentLabel;//会办部门相关人员
@property(nonatomic,weak)IBOutlet UILabel  * officeDepartmentLeaderLabel;//会办部门分管领导
@property(nonatomic,weak)IBOutlet UILabel  * commanderLabel;//总指挥
@property(nonatomic,weak)IBOutlet UILabel  * handleLeaderLabel; //经办部门分管领导
@property(nonatomic,weak)IBOutlet UILabel  * officeLeaderLabel; //会办部门负责人
@property(nonatomic,weak)IBOutlet UILabel  * buildManagerLabel; //建设管理部
@property(nonatomic,weak)IBOutlet UILabel  * officeLabel; //办公室
@property(nonatomic,weak)IBOutlet UILabel  * planDepartmentLabel; //计划财务部
@property(nonatomic,weak)IBOutlet UILabel  * financialDirectorLabel; //财务总监
@property(nonatomic,weak)IBOutlet UILabel  * buildleaderLabel; //建设管理部分管领导
@property(nonatomic,weak)IBOutlet UILabel  * officeOtherLeaderLabel; //办公室分管领导
@property(nonatomic,weak)IBOutlet UILabel  * ceoLabel; //总经理意见
@property(nonatomic,weak)IBOutlet UILabel  * chairMenLabel;//董事长

@property (weak, nonatomic) IBOutlet UILabel *departmentManagerLabel;//部门经理

@property (weak, nonatomic) IBOutlet UILabel *ComandHeadLabel; //指挥部负责人

@property (weak, nonatomic) IBOutlet UITextField *ComandHeadTextField;//指挥部负责人



@property (weak, nonatomic) IBOutlet UITextField *departmentManagerTextField; //部门经理


@property(nonatomic,weak)IBOutlet  UITextField  * finalStaffTextField; //预决算人员
@property(nonatomic,weak)IBOutlet  UITextField  * handleDepartmentTextField; //经办部门负责人
@property(nonatomic,weak)IBOutlet  UITextField  * officeDepartmentTextField;//会办部门相关人员
@property(nonatomic,weak)IBOutlet UITextField  * officeDepartmentLeaderTextField;//会办部门分管领导
@property(nonatomic,weak)IBOutlet UITextField  * commanderTextField;//总指挥
@property(nonatomic,weak)IBOutlet UITextField  * handleLeaderTextField; //经办部门分管领导
@property(nonatomic,weak)IBOutlet UITextField  * officeLeaderTextField; //会办部门负责人
@property(nonatomic,weak)IBOutlet UITextField  * buildManagerTextField; //建设管理部
@property(nonatomic,weak)IBOutlet UITextField  * officeTextField; //办公室
@property(nonatomic,weak)IBOutlet UITextField  * planDepartmentTextField; //计划财务部
@property(nonatomic,weak)IBOutlet UITextField  * financialDirectorTextField; //财务总监
@property(nonatomic,weak)IBOutlet UITextField  * buildleaderTextField; //建设管理部分管领导
@property(nonatomic,weak)IBOutlet UITextField  * officeOtherLeaderTextField; //办公室分管领导
@property(nonatomic,weak)IBOutlet UITextField  * ceoTextField; //总经理意见
@property(nonatomic,weak)IBOutlet UITextField  * chairMenTextField;//董事长
@property(nonatomic,weak)IBOutlet UITextField  * tipsTextField;//备注

@property (weak, nonatomic) IBOutlet UITableViewCell *finalStaffTCell;  //预决算人员 cell
@property (weak, nonatomic) IBOutlet UITableViewCell *departmentManagerCell; //部门经理 cell
@property (weak, nonatomic) IBOutlet UITableViewCell *ComandHeadCell; //指挥部负责人cell
@property (weak, nonatomic) IBOutlet UITableViewCell *handleDepartmentCell; //经办部门负人
@property (weak, nonatomic) IBOutlet UITableViewCell *officeDepartmentCell; //会办部门相关人员
@property (weak, nonatomic) IBOutlet UITableViewCell *officeLeaderCell;//会办部门负责人

@property (weak, nonatomic) IBOutlet UITableViewCell *officeDepartmentLeaderCell;//会办部门分管领导

@property (weak, nonatomic) IBOutlet UITableViewCell *commanderCell;//总指挥

@property(nonatomic,strong)NSString * attacmentURL;


@property(nonatomic,assign)BOOL isEditoring; //编辑中
@property(nonatomic,strong)NSMutableArray * departments;
@property(nonatomic,copy)NSString * infoString; //服务器返回字段 根据该字段跳转不同的界面
@property(nonatomic,copy)NSString   * body;
@property(nonatomic,strong)NSMutableArray * subItems; //选中 需要提交得数据
@end

@implementation HQTableViewController
-(NSMutableArray*)attachmentArray{
    if (_attachmentArray ==nil) {
        _attachmentArray = [NSMutableArray array];
    }
    return _attachmentArray;
}

-(NSMutableArray*)mainattachmentArray{
    if (_mainattachmentArray == nil) {
        _mainattachmentArray = [NSMutableArray array];
    }
    return _mainattachmentArray;
}

-(NSMutableArray*)subItems{
    if (_subItems ==nil) {
        _subItems = [NSMutableArray array];
    }
    return _subItems;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    /*
     htkzf 合同款支付
     表单值cmhthq对应2.14 长睦指挥部合同会签详细列表 xml
     表单值cmhtkzf对应2.13 长睦指挥部合同款支付详细列表 xml
     表单值zzhthq对应2.15 紫之隧道合同会签详细列表 xml
     表单值zzhtkzf对应2.16 紫之隧道合同款支付详细列表 xml
     表单值zbhthq对应2.17 合同会签详细列表 xml
     */
    if ([self.formType isEqualToString:@"cmhthq"]) {
        // self.finalStaffTCell.hidden = YES;  // 隐藏 预决算人员
    }
    
    if ([self.formType isEqualToString:@"zbhthq"] || [self.formType isEqualToString:@"htkzf"]) { //  合同会签
      //  self.finalStaffTCell.hidden = YES;  // 隐藏 预决算人员
        self.ComandHeadCell.hidden = NO; // 指挥部负责人cell
        self.departmentManagerCell.hidden = NO; // 部门经理cell
        self.handleDepartmentCell.hidden = YES;//经办部门负责人
        self.officeDepartmentCell.hidden = YES; //会办部门相关人员
        self.officeLeaderCell.hidden = YES;////会办部门负责人
        self.officeDepartmentLeaderCell.hidden = YES;//会办部门分管领导
        self.commanderCell.hidden = YES;//总指挥
    }
    
    if ([self.formType isEqualToString:@"cmhthq"] || [self.formType isEqualToString:@"cmhthq"] || [self.formType isEqualToString:@"zzhthq"] || [self.formType isEqualToString:@"zbhthq"]) {
        self.ContractNameLabel.text = @"合同类型:";
    }
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingAction:) name:@"kBeginEdit" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveAction) name:@"kSave" object:nil];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendAction) name:@"kSend" object:nil];
    [self loadData];
}


#pragma mark 加载网络数据
-(void)loadData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unId":self.fileId},@{@"userId":userId}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:KGetOAOfficeMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
       
        self.detailModel = [self getDetailModel:JSON];
        
        [self setValueToSubViews];
        
       
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
}

//获取解析得到的数据。
-(ProcessModel *)getDetailModel:(NSString*)dataString;
{
    NSData * data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    ProcessModel * detailModel = [[ProcessModel alloc]init];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    //获取根节点（Users）
    GDataXMLElement *rootElement = [doc rootElement];
    //获取根节点下的节点（User）
    NSArray *rootData= [rootElement elementsForName:@"数据"];
    for (GDataXMLElement *subData in rootData) {
        NSArray * tempArray = [subData elementsForName:@"数据要素"];
        for (GDataXMLElement *data in tempArray) {
            GDataXMLElement *sourceElement = [[data elementsForName:@"主题"] objectAtIndex:0];
            NSString *title = [sourceElement stringValue];
            detailModel.title = title;
            
            detailModel.fileId = self.fileId;
            
            GDataXMLElement *titleElement = [[data elementsForName:@"编号"] objectAtIndex:0];
            NSString *number = [titleElement stringValue];
            detailModel.number = number;
            
 
            
            GDataXMLElement *numberElement = [[data elementsForName:@"发出部门"] objectAtIndex:0];
            NSString *senderDepartment = [numberElement stringValue];
            detailModel.senderDepartment = senderDepartment;
            
            GDataXMLElement *fileIdElement = [[data elementsForName:@"发往部门"] objectAtIndex:0];
            NSString *recerDepartment = [fileIdElement stringValue];
            detailModel.recerDepartment = recerDepartment;
            
      
            GDataXMLElement *receiveDateElement = [[data elementsForName:@"合同类型"] objectAtIndex:0];
            NSString *processType = [receiveDateElement stringValue];
            detailModel.processType = processType;
            
            GDataXMLElement *payDateElement = [[data elementsForName:@"支付类型"] objectAtIndex:0];
            NSString *payType = [payDateElement stringValue];
            detailModel.processType = payType;
            
            
            GDataXMLElement *fileTypeElement = [[data elementsForName:@"紧急程度"] objectAtIndex:0];
            NSString *emergencyLevel = [fileTypeElement stringValue];
            detailModel.emergencyLevel = emergencyLevel;
            
            GDataXMLElement *ComandHeadTypeElement = [[data elementsForName:@"指挥部负责人"] objectAtIndex:0];
            NSString *ComandHead = [ComandHeadTypeElement stringValue];
            detailModel.ComandHead = ComandHead;
            
            GDataXMLElement *departmentManagerElement = [[data elementsForName:@"部门经理"] objectAtIndex:0];
            NSString *departmentManager = [departmentManagerElement stringValue];
            detailModel.departmentManager = departmentManager;
            
            
            
            GDataXMLElement *countElement = [[data elementsForName:@"内容"] objectAtIndex:0];
            NSString *contents = [countElement stringValue];
            detailModel.contents = contents;
            
            
            
            GDataXMLElement *urgencyElement = [[data elementsForName:@"经办人"] objectAtIndex:0];
            NSString *handleMen = [urgencyElement stringValue];
            detailModel.handleMen = handleMen;
            
            
            GDataXMLElement *publicesElement = [[data elementsForName:@"日期"] objectAtIndex:0];
            NSString *date = [publicesElement stringValue];
            detailModel.date = date;
            
      
            
            
            GDataXMLElement *bookerElement = [[data elementsForName:@"备注"] objectAtIndex:0];
            NSString *tips = [bookerElement stringValue];
            detailModel.tips = tips;
            
            
            GDataXMLElement *receivedSortElement= [[data elementsForName:@"预决算人员"] objectAtIndex:0];
            NSString *finalStaff = [receivedSortElement stringValue];
            detailModel.finalStaff = finalStaff;
            
            
            GDataXMLElement *remarksElement= [[data elementsForName:@"经办部门负责人"] objectAtIndex:0];
            NSString *handleDepartment = [remarksElement stringValue];
            detailModel.handleDepartment = handleDepartment;
            
            
            GDataXMLElement *officeDoneElement= [[data elementsForName:@"会办部门相关人员"] objectAtIndex:0];
            NSString *officeDepartment = [officeDoneElement stringValue];
            detailModel.officeDepartment = officeDepartment;
            
            GDataXMLElement *officeDepartmentLeaderElement= [[data elementsForName:@"会办部门分管领导"] objectAtIndex:0];
            NSString *officeDepartmentLeader = [officeDepartmentLeaderElement stringValue];
            detailModel.officeDepartmentLeader = officeDepartmentLeader;
            
            
            
            GDataXMLElement *subjectword= [[data elementsForName:@"总指挥"] objectAtIndex:0];
            NSString *commander = [subjectword stringValue];
            detailModel.commander = commander;
          
            
            GDataXMLElement * leaderScanElement= [[data elementsForName:@"经办部门分管领导"] objectAtIndex:0];
            NSString *handleLeader = [leaderScanElement stringValue];
            detailModel.handleLeader = handleLeader;
            
            
            GDataXMLElement *sugestElement= [[data elementsForName:@"会办部门负责人"] objectAtIndex:0];
            NSString * officeLeader = [sugestElement stringValue];
            detailModel.officeLeader = officeLeader;
            
            GDataXMLElement *setpElement= [[data elementsForName:@"建设管理部"] objectAtIndex:0];
            NSString * buildManager = [setpElement stringValue];
            detailModel.buildManager = buildManager;
            
            
            GDataXMLElement *officeElement= [[data elementsForName:@"办公室"] objectAtIndex:0];
            NSString * office = [officeElement stringValue];
            detailModel.office = office;
            
            
            
            GDataXMLElement *planDepartmentElement= [[data elementsForName:@"计划财务部"] objectAtIndex:0];
            NSString * planDepartment = [planDepartmentElement stringValue];
            detailModel.planDepartment = planDepartment;
            
            
            
            
            GDataXMLElement *financialDirectorElement= [[data elementsForName:@"财务总监"] objectAtIndex:0];
            NSString * financialDirector = [financialDirectorElement stringValue];
            detailModel.financialDirector = financialDirector;
            
            GDataXMLElement *buildleaderElement= [[data elementsForName:@"建设管理部分管领导"] objectAtIndex:0];
            NSString * buildleader = [buildleaderElement stringValue];
            detailModel.buildleader = buildleader;
            
            GDataXMLElement *officeOtherLeaderElement= [[data elementsForName:@"办公室分管领导"] objectAtIndex:0];
            NSString * officeOtherLeader = [officeOtherLeaderElement stringValue];
            detailModel.buildManager = officeOtherLeader;
            
        
            GDataXMLElement *financiaLeaderElement= [[data elementsForName:@"分管财务领导"] objectAtIndex:0];
            NSString * financiaLeader = [financiaLeaderElement stringValue];
            detailModel.financiaLeader = financiaLeader;
            
            GDataXMLElement *ceoElement= [[data elementsForName:@"总经理"] objectAtIndex:0];
            NSString * ceo = [ceoElement stringValue];
            detailModel.ceo = ceo;
            
            
            
            GDataXMLElement *chairMenElement= [[data elementsForName:@"董事长"] objectAtIndex:0];
            NSString * chairMen = [chairMenElement stringValue];
            detailModel.chairMen = chairMen;
            
            GDataXMLElement *opinionElement= [[data elementsForName:@"填写意见环节"] objectAtIndex:0];
            NSString * opinion = [opinionElement stringValue];
            detailModel.opinion = opinion;
  
        }
    }
    
    for (GDataXMLElement * subData in rootData) {
        NSArray * tempArray = [subData elementsForName:@"数据实体"];
        for (GDataXMLElement * element in tempArray) {
            NSArray *array = [element elementsForName:@"附件"];
            for (GDataXMLElement * data in array) {
                OAAttachmentsModel * attachModel = [[OAAttachmentsModel alloc]init];
                GDataXMLElement * nameElement = [[data elementsForName:@"名称"] objectAtIndex:0];
                NSString *  name = [nameElement stringValue];
                attachModel.attachmentsTitle = name;
                
                GDataXMLElement * contentElement = [[data elementsForName:@"内容"] objectAtIndex:0];
                NSString *  content = [contentElement stringValue];
                attachModel.attachmentsURL = content;
                
                [self.attachmentArray addObject:attachModel];
            }
        }
    }
    
    for (GDataXMLElement * subData in rootData) {
        NSArray * tempArray = [subData elementsForName:@"正文实体"];
        for (GDataXMLElement * element in tempArray) {
            NSArray *array = [element elementsForName:@"附件"];
            for (GDataXMLElement * data in array) {
                OAAttachmentsModel * attachModel = [[OAAttachmentsModel alloc]init];
                GDataXMLElement * nameElement = [[data elementsForName:@"名称"] objectAtIndex:0];
                NSString *  name = [nameElement stringValue];
                attachModel.attachmentsTitle = name;
                
                GDataXMLElement * contentElement = [[data elementsForName:@"内容"] objectAtIndex:0];
                NSString *  content = [contentElement stringValue];
                attachModel.attachmentsURL = content;
                
                [self.mainattachmentArray addObject:attachModel];
            }
        }
    }
    
    return detailModel;
    
}

-(void)setValueToSubViews{

    self.titleLabel.text = self.detailModel.title; //主题
    self.numberLabel.text = self.detailModel.number; //编号
    self.senderDepartmentLabel.text = self.detailModel.senderDepartment; //发出部门
    self.recerDepartmentLabel.text = self.detailModel.recerDepartment;//发往部门
    self.processTypeLabel.text = self.detailModel.processType; //合同类型
    self.emergencyLevelLabel.text = self.detailModel.emergencyLevel; //紧急程度
    self.contentsLabel.text = self.detailModel.contents; //内容
    self.tipsLabel.text = self.detailModel.tips; ;//备注
    self.finalStaffLabel.text = self.detailModel.finalStaff;; //预决算人员
    self.handleDepartmentLabel.text = self.detailModel.handleDepartment;; //经办部门负责人
    self.officeDepartmentLabel.text = self.detailModel.officeDepartment;;//会办部门相关人员
    self.officeDepartmentLeaderLabel.text = self.detailModel.officeDepartmentLeader;;//会办部门分管领导
    self.commanderLabel.text = self.detailModel.commander;;//总指挥
    self.handleLeaderLabel.text = self.detailModel.handleLeader;; //经办部门分管领导
    self.officeLeaderLabel.text = self.detailModel.officeLeader;; //会办部门负责人
    self.buildManagerLabel.text = self.detailModel.buildManager;; //建设管理部
    self.officeLabel.text = self.detailModel.office;; //办公室
    self.planDepartmentLabel.text = self.detailModel.planDepartment;; //计划财务部
    self.financialDirectorLabel.text = self.detailModel.financialDirector;; //财务总监
    self.buildleaderLabel.text = self.detailModel.buildleader;; //建设管理部分管领导
    self.officeOtherLeaderLabel.text = self.detailModel.officeOtherLeader;; //办公室分管领导
    self.ceoLabel.text = self.detailModel.ceo;; //总经理意见
    self.chairMenLabel.text = self.detailModel.chairMen;;//董事长
    self.departmentManagerLabel.text = self.detailModel.departmentManager; //部门经理
     self.ComandHeadLabel.text = self.detailModel.ComandHead; //指挥部负责人
    
     [self.tableView reloadData];
}


-(void)editingAction:(NSNotification*)note{
    NSDictionary  * dict = note.userInfo;
    BOOL isHidden =  [dict[@"edit"] isEqualToString:@"Y"] ? NO : YES;
    self.isEditoring = [dict[@"edit"] isEqualToString:@"Y"] ? YES : NO;
    if ([self.detailModel.opinion isEqualToString:@"预决算人员"]) {
       self.finalStaffTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"经办部门负责人"]){
        self.handleDepartmentTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"会办部门相关人员"]){
         self.officeDepartmentTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"会办部门分管领导"]){
         self.officeDepartmentLeaderTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"总指挥"]){
         self.commanderTextField.hidden =isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"经办部门分管领导"]){
         self.handleLeaderTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"会办部门负责人"]){
         self.officeLeaderTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"建设管理部"]){
         self.buildManagerTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"办公室"]){
         self.officeTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"计划财务部"]){
         self.planDepartmentTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"财务总监"]){
         self.financialDirectorTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"建设管理部分管领导"]){
         self.buildleaderTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"办公室分管领导"]){
         self.officeOtherLeaderTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"总经理"]){
        self.ceoTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"董事长"]){
        self.chairMenTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"备注"]){
        self.tipsTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"部门经理"]){
        self.departmentManagerTextField.hidden = isHidden;
    }else if ([self.detailModel.opinion isEqualToString:@"指挥部负责人"]){
        self.commanderTextField.hidden = isHidden;
    }
    [self.tableView reloadData];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
/*
 htkzf 合同款支付
 表单值cmhthq对应2.14 长睦指挥部合同会签详细列表 xml
 表单值cmhtkzf对应2.13 长睦指挥部合同款支付详细列表 xml
 表单值zzhthq对应2.15 紫之隧道合同会签详细列表 xml
 表单值zzhtkzf对应2.16 紫之隧道合同款支付详细列表 xml
 表单值zbhthq对应2.17 合同会签详细列表 xml
 */

- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.textString = textField.text;
    if ([self.detailModel.opinion isEqualToString:@"经办部门负责人"]) {
        
        self.detailModel.handleDepartment =[NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"会办部门相关人员"]){
        
        self.detailModel.officeDepartment = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"会办部门分管领导"]){
        
        self.detailModel.officeDepartmentLeader = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"总指挥"]){
        
        self.detailModel.commander = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"经办部门分管领导"]){
        
        self.detailModel.handleLeader = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"会办部门负责人"]){
        
        self.detailModel.officeLeader = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"建设管理部"]){
        
        self.detailModel.buildManager = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"办公室"]){
        
        self.detailModel.office = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"计划财务部"]){
        
        self.detailModel.planDepartment = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"财务总监"]){
        
        self.detailModel.financialDirector = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"建设管理部分管领导"]){
        
        self.detailModel.buildleader = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"办公室分管领导"]){
        
        self.detailModel.officeOtherLeader = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"总经理"]){
        
        self.detailModel.ceo = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"董事长"]){
        
        self.detailModel.chairMen = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
        
    }else if ([self.detailModel.opinion isEqualToString:@"备注"]){
        
        self.detailModel.tips = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion] ;
    }else if ([self.detailModel.opinion isEqualToString:@"部门经理"]){
    
        self.detailModel.departmentManager = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion];
        
    }else if ([self.detailModel.opinion isEqualToString:@"指挥部负责人"]){
        
        self.detailModel.ComandHead = [NSString stringWithFormat:@"%@(iOS%@)",textField.text,KSystemVersion];
    }
    

}




#pragma mark 发送
-(void)sendAction{
    if (self.detailModel.opinion.length !=0) {
        if (self.textString.length == 0 && self.isEditoring) {
            [self.view makeToast:@"请填写意见"];
            return;
        }
    }
    
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSArray * array = nil;
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    if (self.detailModel !=nil) {
        if ([self.formType isEqualToString:@"cmhthq"]) { //长睦指挥部合同会签
            self.body = [UtilTool stringByPrcessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"cmhtkzf"]){
            self.body = [UtilTool stringByPayProcessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"zzhthq"]){
            self.body = [UtilTool stringByZZProessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"zzhtkzf"]){
            self.body = [UtilTool stringByPayPRocessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"zbhthq"]){
            self.body = [UtilTool stringContractModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"htkzf"]){
            self.body = [UtilTool stringContractPayModel:self.detailModel otherString:[self returnString]];
        }
        
        array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"famxml":self.body}];
    }
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetSubmitWorkMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        self.departments = [self dataWith:JSON];
        if ([self.infoString isEqualToString:@"1044"]) {
            [self.view makeToast:@"1044"];
        }
        if ([self.infoString isEqualToString:@"11"]) { //成功发送，提交下一办理人
            
            [UIAlertView showWithTitle:@"提示" message:@"发送成功" cancelButtonTitle:@"确定" otherButtonTitles:nil tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
                [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
            }];
            
        }else if ([self.infoString isEqualToString:@"12"]) { //返回环节信息
            OASelectDepartmentController * selectVC = [[OASelectDepartmentController alloc]init];
            selectVC.subItems = self.subItems;
            selectVC.body = self.body;
            selectVC.fileId = self.detailModel.fileId;
            selectVC.attachmentArray = self.attachmentArray;
            selectVC.departments = self.departments;
            [self.navigationController pushViewController:selectVC animated:YES];
        }else if ([self.infoString isEqualToString:@"13"]) {//返回人员列表
            
        }else if ([self.infoString isEqualToString:@"14"]){ //返回树
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
    
}


#pragma mark 数据解析

-(NSMutableArray*)dataWith:(NSString*)dataString
{
    NSData * data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableArray *arrData = [[NSMutableArray alloc]init];
    NSMutableArray * subData = [NSMutableArray array];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData: data options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *users = [rootElement children];
    for (int i = 0;i<[users count];i++) {
        GDataXMLElement *ele = [users objectAtIndex:i];
        if ([[ele name] isEqualToString:@"环节"]){
            OADepartmentModel * model = [[OADepartmentModel alloc]init];
            model.department = [[ele attributeForName:@"流向"] stringValue];
            for (int j=0; j<[ele childCount]; j++) {
                GDataXMLElement  *subItem = [ele childAtIndex:j];
                [subData addObject:subItem.stringValue];
                
            }
            [arrData addObject:model];
        }
    }
    self.subItems = subData; //选中 需要提交得数据
    NSArray *infoData= [rootElement elementsForName:@"info"];
    for (int i = 0;i<[infoData count];i++)
    {
        GDataXMLElement *infoElement = [infoData objectAtIndex:i];
        self.infoString = [infoElement stringValue];
    }
    
    return arrData;
}
/*
 
 表单值cmhthq对应2.14 长睦指挥部合同会签详细列表 xml
 表单值cmhtkzf对应2.13 长睦指挥部合同款支付详细列表 xml
 表单值zzhthq对应2.15 紫之隧道合同会签详细列表 xml
 表单值zzhtkzf对应2.16 紫之隧道合同款支付详细列表 xml
 表单值hthq对应2.17 合同会签详细列表 xml
 htkzf 合同款支付
 */

#pragma mark 保存
-(void)saveAction{
    
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSArray * array = nil;
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    if (self.detailModel !=nil) {
        if ([self.formType isEqualToString:@"cmhthq"]) { //长睦指挥部合同会签
            self.body = [UtilTool stringByPrcessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"cmhtkzf"]){
            self.body = [UtilTool stringByPayProcessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"zzhthq"]){
            self.body = [UtilTool stringByZZProessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"zzhtkzf"]){
            self.body = [UtilTool stringByPayPRocessModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"zbhthq"]){
            self.body = [UtilTool stringContractModel:self.detailModel otherString:[self returnString]];
        }else if ([self.formType isEqualToString:@"htkzf"]){
            self.body = [UtilTool stringContractPayModel:self.detailModel otherString:[self returnString]];
        }
       
        array = @[@{@"unid":self.fileId},@{@"userId":userId},@{@"famxml":self.body}];
    }
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetSaveWork success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        [self.tableView makeToast:@"保存成功"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];

}
-(NSString *)returnString
{
    NSString *string = [[NSString alloc]init];
    string = @"&lt;名称&gt;%@&lt;/名称&gt;\n"
    "&lt;内容&gt;%@&lt;/内容&gt;\n";
    
    NSMutableString *str = [NSMutableString string];
    for (int i = 0; i<self.attachmentArray.count; i++) {
        OAAttachmentsModel * attachModel = self.attachmentArray[i];
        [str appendString: [NSString stringWithFormat:string,attachModel.attachmentsTitle,attachModel.attachmentsURL]];
    }
    
    return str;
}

#pragma mark TableView DataSource

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {

        UITableViewCell * cell = [[UITableViewCell alloc] init];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.textColor = [UIColor colorWithRed:85/255.0 green:85/255.0  blue:85/255.0 alpha:1.0];

        OAAttachmentsModel * model = self.mainattachmentArray[indexPath.row];
        cell.textLabel.text = model.attachmentsTitle;
        return cell;
    }else if (indexPath.section == 2){
        UITableViewCell * cell = [[UITableViewCell alloc] init];
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.textColor = [UIColor colorWithRed:85/255.0 green:85/255.0  blue:85/255.0 alpha:1.0];
        OAAttachmentsModel * model = self.attachmentArray[indexPath.row];
        cell.textLabel.text = model.attachmentsTitle;
        return cell;
    }else{
        UITableViewCell * cell  = [super tableView:tableView cellForRowAtIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //cell.backgroundColor = [UIColor grayColor];
        return cell;
    }
  
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 1) {
        return self.mainattachmentArray.count;
    }else if(section == 2){
        return self.attachmentArray.count;
    }
    return [super tableView:tableView numberOfRowsInSection:section];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return [super numberOfSectionsInTableView:tableView];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 9) {
            if (([self.formType isEqualToString:@"cmhthq"]  || [self.formType isEqualToString:@"zbhthq"] || [self.formType isEqualToString:@"htkzf"])) {
                return 0;
            }
            
        }else if (indexPath.row ==7 || indexPath.row == 8){
            if ([self.formType isEqualToString:@"zbhthq"] || [self.formType isEqualToString:@"htkzf"]) {
                return 64;
            }else{
                return 0;
            }
        }else if (indexPath.row == 10 || indexPath.row == 11 || indexPath.row == 12 || indexPath.row == 13 || indexPath.row == 15){
            if ([self.formType isEqualToString:@"zbhthq"] || [self.formType isEqualToString:@"htkzf"]) {
                return 0;
            }
        }
        
        return 64;
    }

    return 44;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1) { //下载附件 查看附件信息
        OAAttachmentsModel * model = self.mainattachmentArray[indexPath.row];
        [self openFileURL:model.attachmentsURL];
    }else if (indexPath.section == 2){
        
        OAAttachmentsModel * model = self.attachmentArray[indexPath.row];
        [self openFileURL:model.attachmentsURL];
    }
    
}

-(void)openFileURL:(NSString*)url{
    NSString * stringurl = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.attacmentURL =  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[url lastPathComponent]]; //附件路径
    if ([self isExtistFilePathWithFileName:[[NSURL URLWithString:stringurl] lastPathComponent]]) { //已经存在附件
        QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
        previewoCntroller.delegate=self;
        previewoCntroller.dataSource=self;
        previewoCntroller.navigationItem.rightBarButtonItem=nil;
        [self.navigationController pushViewController: previewoCntroller animated:YES];
        
    }else{
        MBProgressHUD * hud  =  [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.labelText = @"下载中...";
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringurl]];
    
       [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
           NSString * filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
           [data writeToFile:[filePath stringByAppendingPathComponent:[response.URL lastPathComponent]] atomically:YES];
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
           QLPreviewController *previewoCntroller = [[QLPreviewController alloc] init] ;
           previewoCntroller.delegate=self;
           previewoCntroller.dataSource=self;
           [self.navigationController pushViewController: previewoCntroller animated:YES];
           
        }];
    }
    
}

#pragma mark PreviewDataSource
//第一个方法是设定一组文档浏览的个数，会影响到在文档预览中的上/下一页。
-(NSInteger)numberOfPreviewItemsInPreviewController:(QLPreviewController *)controller {
    return 1;
}
//第二个方法是指定文档预览的文件路径
-(id<QLPreviewItem>)previewController:(QLPreviewController *)controller previewItemAtIndex:(NSInteger)index {
    
    return [NSURL fileURLWithPath:self.attacmentURL];
}

-(BOOL)isExtistFilePathWithFileName:(NSString*)fileName{
    NSString * file = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString * filePath = [file stringByAppendingPathComponent:fileName];
    NSFileManager * fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath] && fileName.length != 0) {
        return YES;
    }else{
        return NO;
    }
    
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
